<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :okAuth="'wms:rd:order:rdOrder:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="80%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm">
      <template #rdOrderRdsList>
        <BasicTable @register="registerRdOrderRdsTable" @row-click="handleRdOrderRdsRowClick">
        <!-- <a-button class="mt-2" @click="handleRdOrderRdsAdd" v-auth="'wms:rd:order:rdOrder:edit'">
          <Icon icon="i-ant-design:plus-circle-outlined" /> {{ t('新增') }}
        </a-button> -->
      </BasicTable>
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsWmsRdOrderRdOrderForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicTable, useTable } from '/@/components/Table';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { RdOrder, rdOrderSave, rdOrderForm } from '/@/api/wms/rd/order/rdOrder';
  import { Table } from 'ant-design-vue';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('wms.rd.order.rdOrder');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<RdOrder>({} as RdOrder);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    // value: record.value.isNewRecord ? t('新增出库订单') : t('编辑出库订单'),
    value:  t('出库订单'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('公司名称'),
      field: 'company.companyName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('发货单号'),
      field: 'fbillno',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
    },
    {
      label: t('发货日期'),
      field: 'fdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('发运方式'),
      field: 'fyfs',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('联系人'),
      field: 'fcontacts',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('电话'),
      field: 'fphone',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('地址'),
      field: 'faddress',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('数量'),
      field: 'fqty',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('重量'),
      field: 'fweight',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('收货单位'),
      field: 'fsuppliername',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('购货单位内码'),
      field: 'fsupplyid',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
    },
    // {
    //   label: t('购货单位编码'),
    //   field: 'fsupplierno',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 64,
    //   },
    // },
    {
      label: t('购货单位名称'),
      field: 'fsuppliername',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('到站'),
      field: 'fdz',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('省份'),
      field: 'fprovince',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('城市'),
      field: 'fcity',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('是否打印'),
      field: 'fifprinteger',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
    },
    {
      label: t('紧急程度'),
      field: 'furgentlevel',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    // {
    //   label: t('备注'),
    //   field: 'fexplanation',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 255,
    //   },
    // },
    {
      label: t('部门'),
      field: 'fdepartment',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('业务员'),
      field: 'femp',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('主管'),
      field: 'fempz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('制单'),
      field: 'fbiller',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('是否打包'),
      field: 'fifpack',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    // {
    //   label: t('来源标识'),
    //   field: 'sourceFlag',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 100,
    //   },
    // },
    // {
    //   label: t('公司编码'),
    //   field: 'companyCode',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 64,
    //   },
    // },
    {
      label: t('物流单号'),
      field: 'wlno',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('物流公司'),
      field: 'wlcompany',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('承运商'),
      field: 'carrier',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('备注'),
      field: 'fexplanation',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('出库订单明细'),
      field: 'rdOrderRdsList',
      component: 'Input',
      colProps: { lg: 24, md: 24 },
      slot: 'rdOrderRdsList',
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 8, md: 24 },
    // dynamicDisabled: true,
    disabled: true,
  });

  const [registerRdOrderRdsTable, rdOrderRdsTable] = useTable({
    // actionColumn: {
    //   width: 60,
    //   actions: (record: Recordable) => [
    //     {
    //       icon: 'i-ant-design:delete-outlined',
    //       color: 'error',
    //       popConfirm: {
    //         title: '是否确认删除',
    //         confirm: handleRdOrderRdsDelete.bind(this, record),
    //       },
    //       auth: 'wms:rd:order:rdOrder:edit',
    //     },
    //   ],
    // },
    rowKey: 'id',
    pagination: false,
    bordered: true,
    size: 'small',
    inset: true,
    showCustomSummary: true,
  });

  async function setRdOrderRdsTableData(_res: Recordable) {
    // 设置发货单行表的列
    rdOrderRdsTable.setColumns([
      {
        title: t('发货单行号'),
        dataIndex: 'fentryid',
        width: 100,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 64,
        },
        editRule: false,
      },
      {
        title: t('订单号'),
        dataIndex: 'forderno',
        width: 130,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 64,
        },
        editRule: false,
      },
      {
        title: t('订单行号'),
        dataIndex: 'forderentryid',
        width: 100,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 20,
        },
        editRule: false,
      },
      // {
      //   title: t('订单主表ID'),
      //   dataIndex: 'forderid',
      //   width: 130,
      //   align: 'left',
      //   editRule: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 64,
      //   },
      //   editRule: false,
      // },
      
      // {
      //   title: t('物料内码'),
      //   dataIndex: 'fitemid',
      //   width: 130,
      //   align: 'left',
      //   editRule: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 100,
      //   },
      //   editRule: false,
      // },
      {
        title: t('物料代码'),
        dataIndex: 'basInv.viewCode',
        width: 150,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('物料名称'),
        dataIndex: 'fitemname',
        width: 160,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 255,
        },
        editRule: false,
      },
      {
        title: t('形象刊'),
        dataIndex: 'freeVO.cfree1',
        width: 130,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('数量'),
        dataIndex: 'fqty',
        width: 90,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 16,
        },
        editRule: false,
        showSummary: true,
      },
      {
        title: t('重量'),
        dataIndex: 'fweight',
        width: 90,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 16,
        },
        editRule: false,
        showSummary: true,
      },
      // {
      //   title: t('备注信息'),
      //   dataIndex: 'fremarks',
      //   width: 130,
      //   align: 'left',
      //   editRule: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 255,
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('仓库ID'),
      //   dataIndex: 'fstockid',
      //   width: 130,
      //   align: 'left',
      //   editRule: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 64,
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('仓库编码'),
      //   dataIndex: 'fstockno',
      //   width: 130,
      //   align: 'left',
      //   editRule: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 64,
      //   },
      //   editRule: false,
      // },
      {
        title: t('仓库'),
        // dataIndex: 'fstockname',
        dataIndex: 'basWare.cwhname',
        width: 130,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 255,
        },
        editRule: false,
      },
      {
        title: t('要求到货日期'),
        dataIndex: 'freturndate',
        width: 150,
        align: 'center',
        editRow: false,
        editComponent: 'DatePicker',
        editComponentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
        editRule: false,
      },
      {
        title: t('是否样书'),
        dataIndex: 'ifys',
        width: 100,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 20,
        },
        editRule: false,
      },
      {
        title: t('累计出库数'),
        dataIndex: 'rdrecords32.sumCkQty',
        width: 100,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 16,
        },
        editRule: false,
        showSummary: true,
      },
      {
        title: t('累计备货数'),
        dataIndex: 'rdrecords32.sumBhQty',
        width: 100,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 16,
        },
        editRule: false,
        showSummary: true,
      },
      {
        title: t('累计拣货数'),
        dataIndex: 'rdrecords32.sumPickQty',
        width: 100,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 16,
        },
        editRule: false,
        showSummary: true,
      },
      {
        title: t('备货状态'),
        dataIndex: 'rdrecords32.bhStatus',
        width: 100,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 20,
        },
        editRule: false,
        dictType: 'jh_bh_status',
      },
      // {
      //   title: t('分组标志'),
      //   dataIndex: 'groupFlag',
      //   width: 130,
      //   align: 'left',
      //   editRule: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 255,
      //   },
      //   editRule: false,
      // },
      
      // {
      //   title: t('自由项2'),
      //   dataIndex: 'cfree2',
      //   width: 130,
      //   align: 'left',
      //   editRule: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 100,
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('自由项3'),
      //   dataIndex: 'cfree3',
      //   width: 130,
      //   align: 'left',
      //   editRule: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 100,
      //   },
      //   editRule: false,
      // },
    ]);
    rdOrderRdsTable.setTableData(record.value.rdOrderRdsList || []);
  }

  function handleRdOrderRdsRowClick(record: Recordable) {
    record.onEdit?.(true, false);
  }

  function handleRdOrderRdsAdd() {
    rdOrderRdsTable.insertTableDataRecord({
      id: new Date().getTime(),
      isNewRecord: true,
      editable: true,
    });
  }

  function handleRdOrderRdsDelete(record: Recordable) {
    rdOrderRdsTable.deleteTableDataRecord(record);
  }

  async function getRdOrderRdsList() {
    let rdOrderRdsListValid = true;
    let rdOrderRdsList: Recordable[] = [];
    for (const record of rdOrderRdsTable.getDataSource()) {
      if (!(await record.onEdit?.(false, true))) {
        rdOrderRdsListValid = false;
      }
      rdOrderRdsList.push({
        ...record,
        id: !!record.isNewRecord ? '' : record.id,
      });
    }
    for (const record of rdOrderRdsTable.getDelDataSource()) {
      if (!!record.isNewRecord) continue;
      rdOrderRdsList.push({
        ...record,
        status: '1',
      });
    }
    if (!rdOrderRdsListValid) {
      throw { errorFields: [{ name: ['rdOrderRdsList'] }] };
    }
    return rdOrderRdsList;
  }

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await rdOrderForm(data);
    record.value = (res.rdOrder || {}) as RdOrder;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setRdOrderRdsTableData(res);
    updateSchema([
      {
        field: 'djno',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
    ]);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        djno: record.value.djno,
      };
      data.rdOrderRdsList = await getRdOrderRdsList();
      // console.log('submit', params, data, record);
      const res = await rdOrderSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
